package Tree;

/**
 * 作者:hfj
 * 功能:112.路径总和
 * 日期: 2025/10/27 9:04
 */
public class HasPathSum {

    //输出从根节点到叶子节点上是否存在总和为targetSum的路径
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null){
            return false;
        }
        if (root.left == null && root.right == null){
            return targetSum - root.val == 0;
        }
        return hasPathSum(root.left,targetSum - root.val) || hasPathSum(root.right,targetSum - root.val);
    }
}
